﻿using System.Drawing;
using JetBrains.CommonControls;
using JetBrains.ReSharper.CodeView.TreePsiBrowser;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.UnitTestExplorer;
using JetBrains.TreeModels;
using JetBrains.UI.TreeView;
using Objectware.Silverlight.Testing.Runner.Resharper.Elements;

namespace Objectware.Silverlight.Testing.Runner.Resharper
{
	public class BrowserPresenter : TreeModelBrowserPresenter
	{
        public BrowserPresenter()
        {
            Present<ClassTestElement>(PresentTestFixture);
            Present<MethodTestElement>(PresentTest);
        }


        protected override bool IsNaturalParent(object parentValue,
                                                object childValue)
        {
            var @namespace = parentValue as UnitTestNamespace;
            var test = childValue as ClassTestElement;

        	bool isNaturalParent = false;

			if (test != null && @namespace != null)
			{
				isNaturalParent = @namespace.Equals(test.GetNamespace());
				return isNaturalParent;
			}

        	isNaturalParent = base.IsNaturalParent(parentValue, childValue);
        	return isNaturalParent;
        }

        protected virtual void PresentTest(MethodTestElement value,
                                           IPresentableItem item,
                                           TreeModelNode modelNode,
                                           PresentationState state)
        {
			if (value.Class.GetTypeClrName() != value.GetTypeClrName())
			{
				item.RichText = string.Format("{0}.{1}", new CLRTypeName(value.GetTypeClrName()).ShortName, value.MethodName);
			}
			else
			{
				item.RichText = value.MethodName;
			}

			if (value.IsExplicit)
			{
				item.RichText.SetForeColor(SystemColors.GrayText);
			}

        	Image stateImage = UnitTestManager.GetStateImage(state);
            Image typeImage = UnitTestManager.GetStandardImage(UnitTestElementImage.Test);

			if (stateImage != null)
			{
				item.Images.Add(stateImage);
			}
			else if (typeImage != null)
			{
				item.Images.Add(typeImage);
			}
        }

        protected virtual void PresentTestFixture(ClassTestElement value,
                                                  IPresentableItem item,
                                                  TreeModelNode modelNode,
                                                  PresentationState state)
        {
            CLRTypeName name = new CLRTypeName(value.GetTypeClrName());

			if (IsNodeParentNatural(modelNode, value))
			{
				item.RichText = name.ShortName;
			}
			else
			{
				if (string.IsNullOrEmpty(name.NamespaceName))
				{
					item.RichText = name.ShortName;
				}
				else
				{
					item.RichText = string.Format("{0}.{1}", name.NamespaceName, name.ShortName);
				}
			}

            Image stateImage = UnitTestManager.GetStateImage(state);
            Image typeImage = UnitTestManager.GetStandardImage(UnitTestElementImage.TestContainer);

			if (stateImage != null)
			{
				item.Images.Add(stateImage);
			}
			else if (typeImage != null)
			{
				item.Images.Add(typeImage);
			}

        	AppendOccurencesCount(item, modelNode, "test");
        }
	}
}
